using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data
{
    using System.Data.SqlClient;
    using SharpSoft.Data.GSQL;
    using System.ComponentModel;
    /// <summary>
    /// SqlServer数据库
    /// </summary>
    [DisplayName("Sql Server数据库")]
    public class MsSql : Database
    {
        public MsSql(string connstr) : base(SqlClientFactory.Instance, connstr)
        {
        }
        public static Database CreateSchema(string defaultConnStr, string schemaName)
        {
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(defaultConnStr);
            var sql = $"CREATE DATABASE {schemaName};";
            System.Data.SqlClient.SqlConnection connection = new SqlConnection(defaultConnStr);
            connection.Open();
            SqlCommand command = new SqlCommand(sql, connection);
            command.ExecuteNonQuery();
            builder.InitialCatalog = schemaName;
            return new MsSql(builder.ToString());
        }


        public static string CreateConnectionString(string server, int port, string defdb, string uid, string pwd)
        {
            return $"Data Source={server}{(port <= 0 ? "" : "," + port.ToString())};Initial Catalog={defdb};User ID={uid};Password={pwd};";
        }

        public override SQLTextGenerator SQLTextGenerator()
        {
            return new MsSqlTextGenerator();
        }

        public override void Dispose()
        {
            if (this.ClearPoolWhenDispose)
            {
                System.Data.SqlClient.SqlConnection.ClearPool((System.Data.SqlClient.SqlConnection)this.conn);
            }
            base.Dispose();
        }

        public override SqlPagination<TModel> ExecutePagination<TModel>(NativeSQLCommandScript select, int onepagerowscount, int curpageindex, object paras = null)
        {//TODO:实现Sql Server原生语句的分页查询
            throw new NotImplementedException();
        }
        //public override SqlSettingBase Setting()
        //{
        //    return   MsSqlSetting.Default;
        //}
    }
}
